package com.baosight.risk.web.fltd;

import java.util.List;

import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baosight.risk.api.fltd.FltdProcessService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.service.fltd.vo.FltdProcessVO;
import com.baosight.risk.service.fltd.vo.FltdTaskVO;
import com.baosight.risk.service.xtzy.entity.XtzyUser;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 作者： lishaoqing
 * 时间： 2019年07月19日
 * 描述： FltdProcessController
 * 版本: v1.0
 */
@Api(value = "流程实例", description = "流程实例", produces = MediaType.APPLICATION_JSON)
@Slf4j
@RestController
@RequestMapping("/fltd/process")
public class FltdProcessController extends BaseController {

    @Autowired
    private FltdProcessService fltdProcessService;

    @ApiOperation("启动一个流程")
    @PostMapping("startProcess")
    public AjaxResult startProcess(@RequestBody FltdTaskVO vo) throws Exception {
        XtzyUser user = getUser();
        if (user == null || user.getId() == null) {
            return AjaxResult.error("请先登录！");
        }
        fltdProcessService.doStart(vo);
        return AjaxResult.success();
    }

    @ApiOperation("流程提交")
    @PostMapping("submit")
    public AjaxResult submit(@RequestBody FltdTaskVO taskVO) throws Exception {
        XtzyUser user = getUser();
        if (user == null || user.getId() == null) {
            return AjaxResult.error("请先登录！");
        }
        try {
			fltdProcessService.doSubmit(user, taskVO);
			return success();
		} catch (Exception e) {
			return error(e.getMessage());
		}
    }

    @ApiOperation("流程汇总提交")
    @PostMapping("groupSubmit")
    public AjaxResult groupSubmit(@RequestBody List<FltdTaskVO> taskVOList) throws Exception {
        XtzyUser user = getUser();
        if (user == null || user.getId() == null) {
            return AjaxResult.error("请先登录！");
        }

        for (FltdTaskVO taskVO : taskVOList) {
            if (taskVO != null) {
                fltdProcessService.doSubmit(user, taskVO);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 待办移交
     */
    @ApiOperation("待办移交")
    @PostMapping("reassign")
    public AjaxResult reassign(@RequestBody FltdTaskVO taskVO) throws Exception {
        XtzyUser user = getUser();
        if (user == null || user.getId() == null) {
            return AjaxResult.error("请先登录！");
        }
        fltdProcessService.doReassign(user, taskVO);
        return AjaxResult.success();
    }

    @ApiOperation("流程撤回")
    @PostMapping("cancel")
    public AjaxResult cancel(@RequestBody FltdTaskVO taskVO) throws Exception {
        XtzyUser user = getUser();
        if (user == null || user.getId() == null) {
            return AjaxResult.error("请先登录！");
        }
        fltdProcessService.doCancel(user, taskVO);
        return AjaxResult.success();
    }

    @ApiOperation("查询所有流程实例")
    @GetMapping("list")
    public AjaxResult list(
            @RequestParam(value = "flowNo", required = false) String flowNo,
            @RequestParam(value = "bizName", required = false) String bizName,
            @RequestParam(value = "nodeName", required = false) String nodeName,
            @RequestParam(value = "compName", required = false) String compName,
            @RequestParam(value = "deptName", required = false) String deptName,
            @RequestParam(value = "acctYear", required = false) String acctYear,
            @RequestParam(value = "quarterCode", required = false) String quarterCode
    ) {
        startPage("create_time", "desc");
        FltdProcessVO vo = new FltdProcessVO();
        vo.setFlowNo(flowNo);
        vo.setBizName(bizName);
        vo.setNodeName(nodeName);
        vo.setCompName(compName);
        vo.setDeptName(deptName);
        vo.setAcctYear(acctYear);
        vo.setQuarterCode(quarterCode);
        return getResultPage(fltdProcessService.list(vo));
    }

    @ApiOperation("查询所有流程定义")
    @GetMapping("process")
    public AjaxResult getAllProcess() {
        return AjaxResult.success("查询成功", fltdProcessService.getAllProcess());
    }

    @ApiOperation("查询流程节点提交策略")
    @GetMapping("decideType")
    public AjaxResult getDecideType(String processDefId, String nodeKey) {
        return AjaxResult.success("查询成功", fltdProcessService.selectDecideType(processDefId, nodeKey));
    }

    @GetMapping("selectProcessByBizGuid")
    @ApiOperation("根据业务主键查找流程")
    public AjaxResult selectProcessByBizGuid(String bizGuid) {
    	try {
    		return success(fltdProcessService.selectProcessByBizGuid(bizGuid));
		} catch (Exception e) {
			return error(e.getMessage());
		}
    }
}
